package com.ytjj.quartz.task;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.mall.mapper.ReissueLogisticsMapper;
import com.ytjj.qmyx.mall.model.ReissueLogistics;
import com.ytjj.quartz.config.SupplyApiConfig;
import com.ytjj.quartz.model.ReissueRefuseRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class ReissueLogisticsTask {

    @Resource
    private ReissueLogisticsMapper reissueLogisticsMapper;
    @Resource
    private SupplyApiConfig supplyApiConfig;


    /**
     * 1个小时执行一次
     * 补单类型为【换货，检测，返修】: 七天内未填写退货单号，补单系统自动拒绝补单。
     *
     */
    @Scheduled(cron = "0 0 * * * ?")
    public void repairPrepaidOrders(){
        List<ReissueLogistics> logistics = reissueLogisticsMapper.queryWeekReissueLogistics();
        if (!CollectionUtils.isEmpty(logistics)) {
            List<Integer> idList = logistics.stream().map(ReissueLogistics::getId).collect(Collectors.toList());
            reissueLogisticsMapper.refuseStatusAndReason(idList,"七天内未填写退货单号，补单系统自动拒绝补单!");
            ReissueRefuseRequest request = new ReissueRefuseRequest();
            request.setRefuseReason("七天内未填写退货单号，补单系统自动拒绝补单!");
            request.setReissueLogisticsIdList(idList);
             HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/reissueLogistics/refuseListStatus",
                    JSONObject.toJSONString(request), supplyApiConfig.getHeadsMap());
        }
    }

}
